package com.zoro.dyformservice.business.user.controller;

import com.zoro.dyformservice.business.user.service.UserInfoModifyService;
import com.zoro.dyformservice.business.user.entity.UserInfoEntity;
import com.zoro.dyformservice.business.user.entity.UserInfoExtEntity;
import com.zoro.dyformservice.support.response.CommonResponseFormat;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user-modify")
@CommonResponseFormat
public class UserInfoModifyController {
    private final UserInfoModifyService userInfoModifyService;

    public UserInfoModifyController(UserInfoModifyService userInfoModifyService) {
        this.userInfoModifyService = userInfoModifyService;
    }

    @PostMapping("/save")
    public UserInfoEntity save(@RequestBody UserInfoEntity userInfo) {

        UserInfoEntity result = this.userInfoModifyService.save(userInfo);
        UserInfoExtEntity extEntity = result.getUserInfoExt();
        if (extEntity != null) {
            // 如果不设置为null ,就会两个对象互相引用，这会导致序列化时 StackOverflowError
            extEntity.setUserInfo(null);
        }
        return result;
    }

    @GetMapping("/test")
    public void test() {
        this.userInfoModifyService.test();
    }
}
